package com.dengdai.applibrary.base;

import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.dengdai.applibrary.R;
import com.dengdai.applibrary.adapter.BasicDataAdapter;
import com.dengdai.applibrary.network.NetWork;
import com.dengdai.applibrary.task.TaskListener;
import com.dengdai.applibrary.view.custom.LoadDataEemptyView;
import com.dengdai.pullrefresh.library.PullToRefreshBase;
import com.dengdai.pullrefresh.library.PullToRefreshListView;

import java.util.List;

/**
 * @说明 列表基础类
 * @作者 LY
 * @时间 15/12/21 16:31
 * @版权 Copyright(c) 2015 LY-版权所有
 */
public abstract class BaseListFragment<T> extends BaseFragment implements IBaseList {
    private PullToRefreshListView pull_listview;
    public LoadDataEemptyView load_data_view;
    private BasicDataAdapter basicDataAdapter;
    public BaseAction baseAction;
    public BaseViewHolder viewHolder;
    private int page = 0;


    @Override
    public int getLayoutResID() {
        return R.layout.common_base_list;
    }

    @Override
    public void initView(View view) {
        pull_listview = (PullToRefreshListView) view.findViewById(R.id.pull_listview);
        load_data_view = (LoadDataEemptyView) view.findViewById(R.id.load_data_view);
    }

    @Override
    public void business(Context context) {
        viewHolder = getViewHolder();
        basicDataAdapter = new BasicDataAdapter(viewHolder);
        pull_listview.getRefreshableView().setAdapter(basicDataAdapter);
        page = getResources().getInteger(R.integer.default_page);

        if (getListViewMode() != null) {
            pull_listview.setMode(getListViewMode());
        } else {
            pull_listview.setMode(BOTH);
        }
        baseAction = new BaseAction(context) {
            @Override
            protected void doInbackground() {
                loadDataCallBack();
            }
        };

        baseAction.setTaskListener(new TaskListener() {
            @Override
            public void onPreExecute() {
            }

            @Override
            public void onPostExecute() {
                List<T> list = doHandelData(baseAction.getData());
                if (list != null && list.size() > 0) {
                    if (page == getResources().getInteger(R.integer.default_page)) {
                        basicDataAdapter.clear();
                    }
                    page++;
                    basicDataAdapter.setList(list);
                } else {
                    if (page == getResources().getInteger(R.integer.default_page)) {
                        basicDataAdapter.clear();
                    }
                }
                if (basicDataAdapter.getCount() > 0) {
                    load_data_view.setVisibility(View.GONE);
                } else {
                    load_data_view.setVisibility(View.VISIBLE);
                    if (NetWork.getNetworkStatus(getActivity())) {
                        load_data_view.loadDataError();
                    } else {
                        load_data_view.netWorkError();
                    }
                }
                stopRefreshView();
            }
        });
        //刷新和加载
        pull_listview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                if (pull_listview.isRefreshData()) {
                    page = getResources().getInteger(R.integer.default_page);
                }
                loadData();
            }
        });
        //点击事件
        pull_listview.getRefreshableView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                itemClick(basicDataAdapter.getListData().get(position - 1), position - 1);
            }
        });

        load_data_view.setOnLoadDataListener(new LoadDataEemptyView.LoadDataListener() {
            @Override
            public void load() {
                loadData();
            }

            @Override
            public void netWork() {

            }
        });
        //加载数据
        {
            load_data_view.setVisibility(View.VISIBLE);
            if (NetWork.getNetworkStatus(context)) {
                load_data_view.loadData();
                loadData();
            } else {
                load_data_view.netWorkError();
            }
        }

    }


    @Override
    public void loadData() {
        baseAction.execute();
    }

    @Override
    public void stopRefreshView() {
        pull_listview.onRefreshComplete();
    }
}
